package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.Score;
import cn.kgc.springboot.http.ResponseResult;
import cn.kgc.springboot.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2025/1/8
 * @description 成绩服务控制层
 */
@RestController
@RequestMapping("score")
@CrossOrigin
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    // 分页
    @PostMapping("page")
    public ResponseResult scorePage(@RequestBody HashMap<String, Object> params) {

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");

        Object studentName = params.get("studentName");
        Object clazz = params.get("clazz");
        Object examContent = params.get("examContent");


        Page<Score> scorePage = new Page<>(page, limit);

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(ObjectUtils.isNotNull(studentName), Score::getStudentName, studentName)
                .eq(ObjectUtils.isNotNull(clazz), Score::getClazz, clazz)
                .eq(ObjectUtils.isNotNull(examContent), Score::getExamContent, examContent)
                .orderByDesc(Score::getExamTime);

        scoreService.page(scorePage, lambda);

        List<Score> records = scorePage.getRecords();
        long total = scorePage.getTotal();


        return ResponseResult.success().data("total", total).data("tableData", records);
    }

    // 添加 或修改
    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdate(@RequestBody Score score) {
        // saveOrUpdate 方法后续会根据实体类中有没有携带id属性 选择是添加还是更新数据  id有值则修改  没有值则添加
        scoreService.saveOrUpdate(score);
        return ResponseResult.success();
    }

    // 删除
    @PostMapping("batchDelete")
    public ResponseResult batchDelete(@RequestBody List<Integer> ids) {

        scoreService.removeByIds(ids);
        return  ResponseResult.success();
    }


}
